home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!ns.unibol.com
- From: John Girvin <jgirvin@bfs.unibol.com>
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Collision Detects !!!!
- Date: Sun, 31 Mar 1996 13:57:23 GMT
- Message-ID: <199603311357.NAA27594@mailhost.unibol.com>
- X-NNTP-Posting-Host: ns.unibol.com
- X-Newsreader: TIN [version 1.2 PL2]
- X-Mail2News-Path: ns.unibol.com
-
- On Sat, 30 Mar 1996 17:31:02 GMT, breese@imada.ou.dk (Bjorn Reese) wrote:
- :>Cezary Kobylinski (amigos@safona.tuniv.szczecin.pl) wrote:
- :>> I want to know what is the best method to detect collision between any
- :>> bobs ?!
-
- :>Use coordinate check as a quick and approximate detection.
-
- :>Assuming that (xpos,ypos) is the upper left corner of the bob,
- :>it could look something like this (must be done for each pair
- :>of a's an b's)
-
- :> if (a->flags.solid &&
- :> (a->xpos < b->xpos + b->xsize) &&
- :> (a->xpos + a->xsize > b->xpos ) &&
- :> (a->ypos < b->ypos + b->ysize) &&
- :> (a->ypos + a->ysize > b->ypos )) {
- :> /* Overlap */
- :> }
-
- Theres a method that uses unsigned compares and requires only 2
- comparisons per object pair. It does require you to store or
- calculate the coordinates of the bottom right hand corner of your
- bounding rectangle for *either* "a" objects or "b" objects, so
- the speedup may be minimal in some cases. However, for situations
- where you are checking if one "a" object (eg: player) has collided
- with loads of other objects (eg: enemy bullets) you can get a good
- speedup by pre-calcing the player BRHC outside the loop.
-
- The pseudo code looks like this:
-
- -------------------8<-----------------------------------------------
- px2 = player->x + player->width // PreCalc player BRHC coords.
- py2 = player->y + player->height
-
- for (i=1; i<num_bullets; i++)
- {
- if ( (bullet[i]->x - px2) < (bullet[i]->width + player->width) &&
- (bullet[i]->y - py2) < (bullet[i]->height + player->height) )
- {
- // Collision!
- // Call your player_was_hit() function.
- }
- }
- -------------------8<-----------------------------------------------
-
- All comparisons and variables are ***UNSIGNED***. It doesnt work if you
- use signed arithmetic!
-
- Note that (bullet->width+player->width) and (bullet->width+player->width)
- are constants inside the loop (best place for constants, IMHO ;) if all the
- bullets are the same size. Obvious optimisation #1
-
- A not-so-obvious optimisation depends on the shape of your playing
- area. If its tall and thin (eg: vertical scroller) then it is more
- likely that the Y-coords will be "out of range" than the X coords,
- so you should shift the Y comparison to be first in order to be more
- likely to discard non-collisions on the first comparison. Vice-versa
- if your playing area is wider than it is tall.
-
- I have 68k code for many instances of this type of collision detection.
- Mail me if you want it or would like it posted here, or if any of this
- rambling confuses you :)
-
- Also, look for references on "sector based collision detection",
- especially around rec.games.programmer archives (HINT!). It might
- help you more if you have lots of objects to check.
-
- Well, I hope that 0.02c helped!
-
- cya,
- /John.
- __________________________________________________________________________
- |/\John Girvin : developing software for Unibol Inc., speaking for myself|
- |\/jgirvin@bfs.unibol.com | Amiga,!PC,net,Trek,SF,MTB,C2H50H,house,techno|
- |girv@girvnet.demon.co.uk | Youll never take me alive, Macro$loth fiends!|
- \A1200/030-40/10M/3.0 A500/000-7/2M/2.04 464/Z80-4/0.0625M/1.0 Team AMIGA/
-